home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ibus / bus.py < prev    next >
Text File  |  2009-11-05  |  5KB  |  179 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.         "Bus",
  24.     )
  25.  
  26. import dbus
  27. import dbus.lowlevel
  28. import dbus.connection
  29. import dbus.mainloop.glib
  30. import gobject
  31. import common
  32. import object
  33. import serializable
  34. import config
  35.  
  36. dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
  37.  
  38. class Bus(object.Object):
  39.     __gtype_name__ = "PYIBusBus"
  40.     __gsignals__ = {
  41.         "disconnected" : (
  42.             gobject.SIGNAL_RUN_LAST,
  43.             gobject.TYPE_NONE,
  44.             ()
  45.         ),
  46.         "config-reloaded" : (
  47.             gobject.SIGNAL_RUN_LAST,
  48.             gobject.TYPE_NONE,
  49.             ()
  50.         ),
  51.     }
  52.  
  53.     def __init__(self):
  54.         super(Bus, self).__init__()
  55.         self.__dbusconn = dbus.connection.Connection(common.get_address())
  56.         self.__dbus = self.__dbusconn.get_object(dbus.BUS_DAEMON_NAME,
  57.                                                  dbus.BUS_DAEMON_PATH)
  58.         self.__unique_name = self.hello()
  59.         self.__ibus = self.__dbusconn.get_object(common.IBUS_SERVICE_IBUS,
  60.                                                  common.IBUS_PATH_IBUS)
  61.  
  62.         self.__dbusconn.call_on_disconnection(self.__dbusconn_disconnected_cb)
  63.         # self.__dbusconn.add_message_filter(self.__filter_cb)
  64.  
  65.     def __filter_cb(self, conn, message):
  66.         if message.get_type() == 4:
  67.             print "Signal %s" % message.get_member()
  68.             print " sender = %s" % message.get_sender()
  69.             print " path = %s" % message.get_path()
  70.         return dbus.lowlevel.HANDLER_RESULT_NOT_YET_HANDLED
  71.  
  72.     def __dbusconn_disconnected_cb(self, dbusconn):
  73.         assert self.__dbusconn == dbusconn
  74.         self.__dbusconn = None
  75.         self.emit("disconnected")
  76.  
  77.     def get_name(self):
  78.         return self.__unique_name
  79.  
  80.     def get_is_connected(self):
  81.         if self.__dbusconn == None:
  82.             return False
  83.         return self.__dbusconn.get_is_connected()
  84.  
  85.     # define dbus methods
  86.     def get_dbus(self):
  87.         return self.__dbus
  88.  
  89.     def hello(self):
  90.         return self.__dbus.Hello()
  91.  
  92.     def request_name(self, name, flags):
  93.         return self.__dbus.RequestName(name, dbus.UInt32 (flags))
  94.  
  95.     def release_name(self, name):
  96.         return self.__dbus.ReleaseName(name)
  97.  
  98.     def get_name_owner(self, name):
  99.         return self.__dbus.GetNameOwner(name)
  100.  
  101.     def add_match(self, rule):
  102.         return self.__dbus.AddMatch(rule)
  103.  
  104.     def remove_match(self, rule):
  105.         return self.__dbus.RemoveMatch(rule)
  106.  
  107.     def get_dbusconn(self):
  108.         return self.__dbusconn
  109.  
  110.     def get_address(self):
  111.         return common.get_address()
  112.  
  113.     # define ibus methods
  114.     def register_component(self, component):
  115.         component = serializable.serialize_object(component)
  116.         return self.__ibus.RegisterComponent(component)
  117.  
  118.     def list_engines(self):
  119.         engines = self.__ibus.ListEngines()
  120.         return map(serializable.deserialize_object, engines)
  121.  
  122.     def list_active_engines(self):
  123.         engines = self.__ibus.ListActiveEngines()
  124.         return map(serializable.deserialize_object, engines)
  125.  
  126.     def create_input_context(self, client_name):
  127.         return self.__ibus.CreateInputContext(client_name)
  128.  
  129.     def exit(self, restart):
  130.         return self.__ibus.Exit(restart)
  131.  
  132.     def ping(self, data):
  133.         flag = isinstance(data, serializable.Serializable)
  134.         if flag:
  135.             data = serializable.serialize_object(data)
  136.         data = self.__ibus.Ping(data, dbus_interface="org.freedesktop.IBus")
  137.         if flag:
  138.             data = serializable.deserialize_object(data)
  139.         return data
  140.  
  141.     def introspect_ibus(self):
  142.         return self.__ibus.Introspect()
  143.  
  144.     def introspect_dbus(self):
  145.         return self.__dbus.Introspect()
  146.  
  147.     def get_config(self):
  148.         try:
  149.             return self.__config
  150.         except:
  151.             self.__config = config.Config(self)
  152.             return self.__config
  153.  
  154. def test():
  155.     import glib
  156.     import factory
  157.     import text
  158.  
  159.     mainloop = glib.MainLoop()
  160.  
  161.     def __disconnected_cb(*args):
  162.         print "Disconnected", args
  163.         mainloop.quit()
  164.  
  165.     b = Bus()
  166.     b.connect("disconnected", __disconnected_cb)
  167.  
  168.     print "unique_name =", b.get_name()
  169.  
  170.     for i in b.list_factories():
  171.         print i.name
  172.  
  173.     mainloop.run()
  174.     print "Exit"
  175.  
  176.  
  177. if __name__ == "__main__":
  178.     test()
  179.